package com.example.Opd_ISF_Android_Application.view.activities;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.example.Opd_ISF_Android_Application.R;

import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Dell
 * Date: 27/03/13
 * Time: 21:14
 * To change this template use File | Settings | File Templates.
 */
public class FilterRankingsActivity extends Activity {
    private Button btnShowRankings;
    private Button btnChangeDate;
    static final int DATE_DIALOG_ID = 0;
    private int year;
    private int month;
    private int day;
    private TextView tvDisplayDate;
    private DatePickerDialog.OnDateSetListener datePickerListener;
    private Spinner spinRankingType;
    private boolean isStudentRanking;
    private LinearLayout studentTitles;
    private LinearLayout studentItems;


    public FilterRankingsActivity() {
       isStudentRanking = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        setContentView(R.layout.filter_rankings);
        // set current date
        Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR) - 2; // adjust to current year
        month = cal.get(Calendar.MONTH);
        month = cal.get(Calendar.DAY_OF_MONTH);

        // initialise components & list
        initialiseComponents();
        addEvents();
    }

    private void initialiseComponents() {
        btnShowRankings = (Button) findViewById(R.id.btnGo);
        btnChangeDate = (Button) findViewById(R.id.btnSetDate);
        tvDisplayDate = (TextView) findViewById(R.id.tvDisplayDate);
        spinRankingType = (Spinner) findViewById(R.id.spinRankChooser);
        studentItems = (LinearLayout) findViewById(R.id.studentItems);
        studentTitles = (LinearLayout) findViewById(R.id.studentTitles);
        tvDisplayDate.setText(tvDisplayDate.getText() + " no filter on date");
    }

    private void buttonEvents() {
        btnShowRankings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goToRankings = new Intent(getResources().getString(R.string.rankingsIntent));
                // add data to send to activity
                goToRankings.putExtra("studentRanking", isStudentRanking);
                goToRankings.putExtra("MeetingType", "");   //TODO: Write method that gets all necessary data
                goToRankings.putExtra("Sport", "");
                goToRankings.putExtra("Date", "");
                goToRankings.putExtra("Country", "");
                goToRankings.putExtra("Region", "");
                goToRankings.putExtra("Measure", "");
                goToRankings.putExtra("Discipline", "");
                goToRankings.putExtra("Gender", "");  //TODO: In method that gets data check if these fields are still invisible, if yes then add empty data @method: getDataToPass  see below
                goToRankings.putExtra("Age", "");
                startActivity(goToRankings);
            }
        });

        btnChangeDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DATE_DIALOG_ID);
            }
        });

    }

    private void addEvents() {
        buttonEvents();
        spinnerEvents();

        // date picker box
        datePickerListener
                = new DatePickerDialog.OnDateSetListener() {

            // when dialog box is closed, below method will be called.
            public void onDateSet(DatePicker view, int selectedYear,
                                  int selectedMonth, int selectedDay) {
                year = selectedYear;
                month = selectedMonth;
                day = selectedDay;

                // set selected date into textview
                String selectedDate = new StringBuilder().append(" ").append(month + 1)
                        .append("-").append(day).append("-").append(year)
                        .append(" ").toString();

                tvDisplayDate.setText(selectedDate);

            }
        };
    }

    // Check if teams or students are selected
    private void spinnerEvents() {
        spinRankingType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (l == 1) {
                    showStudentFilterComponents();
                    isStudentRanking = true;
                } else if (l == 0)  {
                    hideStudentFilterComponents();
                    isStudentRanking = false;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        }

        );
    }

    private void hideStudentFilterComponents() {
        studentTitles.setVisibility(8);
        studentItems.setVisibility(8);
    }

    private void showStudentFilterComponents() {
        studentTitles.setVisibility(0);
        studentItems.setVisibility(0);
    }

    private String[] getDataToPass() {
        String[] data = null;
        return data;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                // set date picker as current date
                DatePickerDialog dialog = new DatePickerDialog(this, datePickerListener,
                        year, month, day);
                return dialog;
        }
        return null;
    }


}
